package user

import (
	"encoding/json"
	"vblog/common"

	"golang.org/x/crypto/bcrypt"
)

// 用于存放存入数据库中的数据
type User struct {
	// 通用字段
	*common.Meta
	// 用户传递过来的请求
	*CreateUserRequest
}

func NewUser(req *CreateUserRequest) *User {
	return &User{
		Meta:              common.NewMeta(),
		CreateUserRequest: req,
	}
}

func (u *User) CheckPassword(password string) error {
	return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
}

func (u *User) String() string {
	js, _ := json.Marshal(u)
	return string(js)
}
